class TrainingAppointment < ApplicationRecord
  belongs_to :order, class_name: "CoachOrder"
  belongs_to :training
  before_save {false}

  def self.to_csv
    require 'csv'
    csv_string = CSV.generate(headers: true) do |csv|
      csv << %w"订单号 报名教练 教练手机 训练课程 开始时间 结束时间 状态 订单金额 下单时间"
      self.all.each do |appointment|
        order = appointment.order
        csv << [
            order.order_no,
            order.user_name,
            appointment.coach.contact,
            appointment.training_name,
            appointment.training_begin_time.strftime("%Y-%m-%d %H:%M"),
            appointment.training_end_time.strftime("%Y-%m-%d %H:%M"),
            CoachOrder::STATUSES[order.status.to_sym],
            order.amount,
            order.order_time.strftime("%Y-%m-%d %H:%M")
        ]
      end
    end
    csv_string
  end
end
